package com.parking.mgr.useraccount.facede;

import com.parking.mgr.useraccount.model.entity.BasicAuthCodeEntity;
import com.parking.mgr.useraccount.model.entity.LoginTokenEntity;
import com.parking.mgr.useraccount.model.param.LoginTokenParam;
import com.parking.mgr.useraccount.service.AuthCodeService;
import com.parking.mgr.useraccount.service.LoginPasswordService;
import com.parking.mgr.useraccount.service.LoginTokenService;
import com.parking.mgr.useraccount.service.impl.AuthServiceImpl;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @ClassName LoginServiceFacede
 * @Description 登录有关service 外观模式
 * @Auther: zhijianpu
 * @Date: 2023/6/12
 */
@Component
public class LoginServiceFacede {

    @Resource
    private LoginPasswordService loginPasswordService;

    @Resource
    private LoginTokenService loginTokenService;


    @Resource
    private AuthCodeService authCodeService;


    @Resource
    private AuthServiceImpl authService;

    /**
     * 根据用户账号删除用户密码
     *
     * @param passportId
     */
    public void deletePasswordByPassportId(String passportId) {
        loginPasswordService.deletePasswordByPassportId(passportId);
    }

    /**
     * 登录令牌获取用户账号
     *
     * @param request
     * @return
     */
    public String getPassportIdByToken(HttpServletRequest request) {
        return loginTokenService.getPassportIdByToken(request);
    }

    /**
     *  保存客户端ID
     */
    public void add(LoginTokenParam loginTokenParam, HttpServletRequest request) {
        loginTokenService.add(loginTokenParam, request);
    }

    /**
     * 更token过期时间
     * @param loginTokenParam
     */
    public void updateExpireTime(LoginTokenParam loginTokenParam) {
        loginTokenService.updateExpireTime(loginTokenParam);
    }

    /**
     * 获取loginTokenEntity
     *
     * @param token
     * @param clientId
     * @return
     */
    public LoginTokenEntity getLoginTokenEntity(String token, String clientId) {
        return loginTokenService.getLoginTokenEntity(token, clientId);
    }

    /**
     * 删除token
     *
     * @param token
     * @param clientId
     */
    public void delete(String token, String clientId) {
        loginTokenService.delete(token, clientId);
    }

    /**
     * 更新登录令牌
     *
     * @param token
     * @param passportId
     */
    public void updateToken(String token, String passportId, String clientId) {
        loginTokenService.updateToken(token, passportId, clientId);
    }

    /**
     * 获取登录令牌ID
     *
     * @param token
     * @param clientId
     * @return
     */
    public String getTokenId(String token, String clientId) {
        return loginTokenService.getTokenId(token, clientId);
    }

    /**
     * 查询所属用户账号
     *
     * @param token
     * @param clientId
     * @return
     */
    public String queryPassportByClientIdToken(String token, String clientId) {
        return loginTokenService.queryPassportByClientIdToken(token, clientId);
    }

    /**
     * 查询用户是否登录
     *
     * @param request
     * @return
     */
    public boolean isLoginAuth(HttpServletRequest request) {
        return loginTokenService.isLoginAuth(request);
    }

    /**
     * 根据ID获取验证码
     *
     * @param id
     * @return
     */
    public String getAuthCodeById(String id) {
        return authCodeService.getAuthCodeById(id);
    }

    /**
     * 通过clientId获取验证实体
     *
     * @param clientId 客户端ID
     * @return 验证码
     */
    public BasicAuthCodeEntity getAuthCodeEntityByClientId(String clientId) {
        return authCodeService.getAuthCodeEntityByClientId(clientId);
    }

    /**
     * 验证码失败，更新验证码状态和验证结果
     *
     * @param id
     * @param passportId
     * @param clientId
     */
    public void updateAuthCodeByAuthFail(String id, String passportId, String clientId) {
        authCodeService.updateAuthCodeByAuthFail(id, passportId, clientId);
    }

    /**
     * 验证码成功，更新验证码状态和验证结果，同时迁移到历史表
     *
     * @param id
     * @param passportId
     * @param clientId
     * @param loginToken
     */
    public void updateAuthCodeByAuthSuccess(String id, String passportId, String clientId, String loginToken) {
        authCodeService.updateAuthCodeByAuthSuccess(id, passportId, clientId, loginToken);
    }

    /**
     * 通过用户账号ID查询登录令牌实体
     *
     * @param passportId 用户账号ID
     * @return 登录令牌实体
     */
    public LoginTokenEntity queryLoginTokenByPassportId(String passportId) {
        return loginTokenService.queryLoginTokenByPassportId(passportId);
    }

    /**
     * 退出登录
     *
     * @param request
     * @return
     */
    public String logout(HttpServletRequest request) {
        return authService.logout(request);
    }

    public void saveLoginToken(LoginTokenEntity loginTokenEntity) {
        loginTokenService.saveOrUpdate(loginTokenEntity);
    }
}
